package cn.newgrand.netcall.consultant.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.sparkimpl.plugin.alerts.SparkToaster;

import cn.newgrand.netcall.consultant.SearchManager;
import cn.newgrand.smackx.consultant.ConsultantManager;

public class PollPanel extends JPanel {
	
	private String spjid;
	private String spid;
	private String sessionId;
    private JLabel question = new JLabel("您的问题解决了吗？");
    private JLabel satisfaction   = new JLabel("您对专家的回答满意吗？");
    
    private ButtonGroup group = new ButtonGroup();
    private ButtonGroup group2 = new ButtonGroup();
    
    private JRadioButton solve   = new JRadioButton("解决");
    private JRadioButton unsolve   = new JRadioButton("未解决");
    private JRadioButton verysatisfy   = new JRadioButton("非常满意");
    private JRadioButton satisfy   = new JRadioButton("满意");
    private JRadioButton general   = new JRadioButton("一般");
    private JRadioButton  unsatisfy = new JRadioButton("不满意");
    
    private PollButton  submit = new PollButton("提交");
    
    private String solveString = "solve";
    private String satisfyString = "verysatisfy";
    
    private SparkToaster toaster;
    
    
	public PollPanel(String spid ,String spjid) {
		// TODO Auto-generated constructor stub
		this.spjid = spjid;
		this.spid = spid;
		
        setLayout(new GridBagLayout());
        setBackground(new Color(250, 249, 242));
        setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.white));
        initializeComponent();
	}
	
	public PollPanel(String spid ,String spjid,String sessionId,SparkToaster toaster) {
		// TODO Auto-generated constructor stub
		this.spjid = spjid;
		this.spid = spid;
		this.toaster = toaster;
		this.sessionId = sessionId;
        setLayout(new GridBagLayout());
        setBackground(new Color(250, 249, 242));
        setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.white));
        initializeComponent();
	}
	
	private void initializeComponent(){
	       add(question, new GridBagConstraints(0, 0, 4, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(solve, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(unsolve, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(satisfaction, new GridBagConstraints(0, 2, 4, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(verysatisfy, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(satisfy, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(general, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(unsatisfy, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        add(submit, new GridBagConstraints(1, 4, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        
	        solve.setForeground(new Color(73, 113, 196));
	        solve.setFont(new Font("Dialog", Font.BOLD, 12));
	        solve.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
	        
	        unsolve.setForeground(new Color(73, 113, 196));
	        unsolve.setFont(new Font("Dialog", Font.BOLD, 12));
	        unsolve.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
	        
	        verysatisfy.setForeground(new Color(73, 113, 196));
	        verysatisfy.setFont(new Font("Dialog", Font.BOLD, 12));
	        verysatisfy.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
	        
	        satisfy.setForeground(new Color(73, 113, 196));
	        satisfy.setFont(new Font("Dialog", Font.BOLD, 12));
	        satisfy.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
	        
	        general.setForeground(new Color(73, 113, 196));
	        general.setFont(new Font("Dialog", Font.BOLD, 12));
	        general.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
	        
	        unsatisfy.setForeground(new Color(73, 113, 196));
	        unsatisfy.setFont(new Font("Dialog", Font.BOLD, 12));
	        unsatisfy.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
	        
	        submit.setForeground(new Color(73, 113, 196));
	        submit.setFont(new Font("Dialog", Font.BOLD, 18));
	        submit.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
	        
	        group.add(solve);
	        group.add(unsolve);
	        group.setSelected(solve.getModel(),true);
	        
	        group2.add(verysatisfy);
	        group2.add(satisfy);
	        group2.add(general);
	        group2.add(unsatisfy);
	        group2.setSelected(verysatisfy.getModel(),true);
	        
	    	ActionListener action = new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					String temp = ((JRadioButton)e.getSource()).getText();
					if(temp.equals("解决"))
						solveString = "solve";
					else if(temp.equals("未解决"))
						solveString = "unsolve";
					else if(temp.equals("非常满意"))
						satisfyString  = "verysatisfy";
					else if(temp.equals("满意"))
						satisfyString = "satisfy";
					else if(temp.equals("一般"))
						satisfyString = "general";
					else if(temp.equals("不满意"))
						satisfyString = "unsatisfy";
				}
	    	};
	    	
	    	solve.addActionListener(action);
	    	unsolve.addActionListener(action);
	    	verysatisfy.addActionListener(action);
	    	satisfy.addActionListener(action);
	    	general.addActionListener(action);
	    	unsatisfy.addActionListener(action);
	        
	        submit.addMouseListener(new MouseAdapter() {
	            public void mouseEntered(MouseEvent e) {
	            	submit.setCursor(new Cursor(Cursor.HAND_CURSOR));
	
	            }
	
	            public void mouseExited(MouseEvent e) {
	            	submit.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	            }
	        });
	        
	        submit.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	            	ConsultantManager.getInstance(SparkManager.getConnection()).sendPollPacket(spid, solveString, satisfyString,sessionId);
	            	toaster.close();
	            	submit.setEnabled(false);
	            }
	        });
	        
//	        solve.addMouseListener(new MouseAdapter() {
//	            public void mouseEntered(MouseEvent e) {
//	            	solve.setCursor(new Cursor(Cursor.HAND_CURSOR));
//
//	            }
//
//	            public void mouseExited(MouseEvent e) {
//	            	solve.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//	            }
//	        });
//	        
//	        unsolve.addMouseListener(new MouseAdapter() {
//	            public void mouseEntered(MouseEvent e) {
//	            	unsolve.setCursor(new Cursor(Cursor.HAND_CURSOR));
//
//	            }
//
//	            public void mouseExited(MouseEvent e) {
//	            	unsolve.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//	            }
//	        });
//	        
//	        verysatisfy.addMouseListener(new MouseAdapter() {
//	            public void mouseEntered(MouseEvent e) {
//	            	verysatisfy.setCursor(new Cursor(Cursor.HAND_CURSOR));
//
//	            }
//
//	            public void mouseExited(MouseEvent e) {
//	            	verysatisfy.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//	            }
//	        });
//	        
//	        satisfy.addMouseListener(new MouseAdapter() {
//	            public void mouseEntered(MouseEvent e) {
//	            	satisfy.setCursor(new Cursor(Cursor.HAND_CURSOR));
//
//	            }
//
//	            public void mouseExited(MouseEvent e) {
//	            	satisfy.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//	            }
//	        });
//	        
//	        general.addMouseListener(new MouseAdapter() {
//	            public void mouseEntered(MouseEvent e) {
//	            	general.setCursor(new Cursor(Cursor.HAND_CURSOR));
//
//	            }
//
//	            public void mouseExited(MouseEvent e) {
//	            	general.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//	            }
//	        });
//	        
//	        unsatisfy.addMouseListener(new MouseAdapter() {
//	            public void mouseEntered(MouseEvent e) {
//	            	unsatisfy.setCursor(new Cursor(Cursor.HAND_CURSOR));
//
//	            }
//
//	            public void mouseExited(MouseEvent e) {
//	            	unsatisfy.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//	            }
//	        });
//	        
//	        solve.addActionListener(new ActionListener() {
//	            public void actionPerformed(ActionEvent e) {
//	            	ConsultantManager.getInstance(SparkManager.getConnection()).sendPollPacket(spid, "solve", null);
//	            	solve.setEnabled(false);
//	            	unsolve.setEnabled(false);
//	            }
//	        });
//	        
//	        unsolve.addActionListener(new ActionListener() {
//	            public void actionPerformed(ActionEvent e) {
//	            	ConsultantManager.getInstance(SparkManager.getConnection()).sendPollPacket(spid, "unsolve", null);
//	            	solve.setEnabled(false);
//	            	unsolve.setEnabled(false);
//	            }
//	        });
//	        
//	        verysatisfy.addActionListener(new ActionListener() {
//	            public void actionPerformed(ActionEvent e) {
//	            	ConsultantManager.getInstance(SparkManager.getConnection()).sendPollPacket(spid, null, "verysatisfy");
//	            	verysatisfy.setEnabled(false);
//	            	satisfy.setEnabled(false);
//	            	general.setEnabled(false);
//	            	unsatisfy.setEnabled(false);
//	            }
//	        });
//	        
//	        satisfy.addActionListener(new ActionListener() {
//	            public void actionPerformed(ActionEvent e) {
//	            	ConsultantManager.getInstance(SparkManager.getConnection()).sendPollPacket(spid, null, "satisfy");
//	            	verysatisfy.setEnabled(false);
//	            	satisfy.setEnabled(false);
//	            	general.setEnabled(false);
//	            	unsatisfy.setEnabled(false);
//	            }
//	        });
//	        
//	        general.addActionListener(new ActionListener() {
//	            public void actionPerformed(ActionEvent e) {
//	            	ConsultantManager.getInstance(SparkManager.getConnection()).sendPollPacket(spid, null, "general");
//	            	verysatisfy.setEnabled(false);
//	            	satisfy.setEnabled(false);
//	            	general.setEnabled(false);
//	            	unsatisfy.setEnabled(false);
//	            }
//	        });
//	        
//	        unsatisfy.addActionListener(new ActionListener() {
//	            public void actionPerformed(ActionEvent e) {
//	            	ConsultantManager.getInstance(SparkManager.getConnection()).sendPollPacket(spid, null, "unsatisfy");
//	            	verysatisfy.setEnabled(false);
//	            	satisfy.setEnabled(false);
//	            	general.setEnabled(false);
//	            	unsatisfy.setEnabled(false);
//	            }
//	        });
	}
	
    private class PollButton extends JButton {

        public PollButton() {
            decorate();
        }

        /**
         * Create a new RolloverButton.
         *
         * @param text the button text.
         * @param icon the button icon.
         */
        public PollButton(String text, Icon icon) {
            super(text, icon);
            decorate();
        }
        
        public PollButton(String text) {
            super(text);
            decorate();
        }

        /**
         * Decorates the button with the approriate UI configurations.
         */
        private void decorate() {
            setBorderPainted(false);
            setOpaque(true);

            setContentAreaFilled(false);
            setMargin(new Insets(1, 1, 1, 1));
        }

    }
}
